<?php
namespace Home\Controller;

use Vendor\Page;

class ShelfController extends ComController{

    public function _initialize()
    {
        parent::_initialize();
    }

    public function Index(){
        $this->is_session();
        //查询并显示书架表中当前用户的数据，默认加入5本热度最高的小说
        $open_id = $_SESSION['user']['open_id'];
        $hot_search = D("Novel")->field('id,name,img,description')->order("click_count desc")->where("is_shelf = 1")->limit("0,5")->select();
        //is_recommend字段:1为平台推荐，2为用户自己加入书架,3为平台推荐但被用户主动删除
        $bookshelf = D("bookshelf")->where("is_recommend != 2 and open_id = '$open_id'")->select();

        if(empty($bookshelf)){
            foreach($hot_search as $kk=>$vv){
                $data['open_id'] = $open_id;
                $data['novel_id'] = $vv['id'];
                $data['novel_name'] = $vv['name'];
                $data['novel_description'] = $vv['description'];
                $data['novel_img'] = $vv['img'];
                $data['addtime'] = time();
                $data['is_recommend'] = 1;
                M("bookshelf")->add($data);
            }
        }
        $book_list = D("bookshelf")->where("open_id = '$open_id' and is_recommend != 3")->order('addtime desc')->select();
        $book_list_count = D("bookshelf")->where("open_id = '$open_id' and is_recommend != 3")->count();
        $this->assign('book_list_count',$book_list_count);
        foreach($book_list as $k=>$v){
            $novel_id = $v['novel_id'];
            $writer_info = D("novel")->field('writer')->where("id = '$novel_id'")->find();
            $writer = $writer_info['writer'];
            $history = D("read_history")->where("novel_id = '$novel_id' and open_id = '$open_id'")->find();
            $history_chapterid = $history['chapter_id'];
            //查出该书籍最后一章的信息
            $latest_chapter = D("novel_chapter")->where("novel_id = '$novel_id'")->order('id desc')->find();
            if (!empty($history)) {
                //查出用户阅读到了第几章
                $book_list[$k]['history_chapter'] = $history['chapter'];
                if ($history_chapterid != $latest_chapter['id']) {
                    $book_list[$k]['dian'] = 1;
                }
                $book_list[$k]['next_chapter_id'] = $history_chapterid;
                $book_list[$k]['latest_chapter_info'] = $latest_chapter['chapter'].' '.$latest_chapter['title'];
                $book_list[$k]['update_time'] = time()-intval($latest_chapter['add_time']);
                $book_list[$k]['writer'] = $writer;
            }else{
                $book_list[$k]['history_chapter'] = '尚未阅读';
                $book_list[$k]['latest_chapter_info'] = $latest_chapter['chapter'].' '.$latest_chapter['title'];
                $book_list[$k]['update_time'] = intval($latest_chapter['add_time']);
                $book_list[$k]['writer'] = $writer;
                $book_list[$k]['dian'] = 1;
            }
        }
        $this->assign("book_list",$book_list);
        $this->display();
    }


    /**
     * [ajaxshelfdel 删除书架的书]
     * @return [type] [description]
     */
    public function ajaxshelfdel()
    {
        $data = $_POST['datas'];
        $bookshelf = M('bookshelf');
        $open_id = $_SESSION['user']['open_id'] ? $_SESSION['user']['open_id'] : $_SESSION['open_id'];
        // is_recommend
        foreach ($data as $v) {
            $is_recommend = $bookshelf->where("novel_id = '$v' and open_id ='$open_id'")->field("is_recommend,id")->find();
            $id = $is_recommend['id'];
            if ($is_recommend['is_recommend'] == 1) {
                $datas['is_recommend'] = 3;
                $result = $bookshelf->where("id = '$id' and open_id ='$open_id'")->save($datas);
            }elseif($is_recommend['is_recommend'] == 2){
                $result = $bookshelf->where("id= '$id' and open_id ='$open_id'")->delete();
            }
        }
        $books = $bookshelf->where("open_id = '$open_id' and is_recommend != 3")->select();
        if (empty($books)) {
            echo 0;
        }else{
            echo 2;
        }
    }

    //加入书籍到书架
    public function addbookshelf()
    {
        $open_id = $_SESSION['user']['open_id'];
        $novel_id = $_POST['novel_id'];
        $data['open_id'] = $open_id;
        $data['novel_id'] = $novel_id;
        $data['novel_name'] = $_POST['novel_name'];
        $data['addtime'] = time();
        $data['novel_description'] = $_POST['novel_description'];
        $data['novel_img'] = $_POST['novel_img'];
        $data['is_recommend'] = 2;
        $shelfbooks = D("bookshelf")->where("open_id = '$open_id' and novel_id = '$novel_id'")->find();
        $shelfbooksId = $shelfbooks['id'];
        //若用户要添加的书籍在平台推荐内，则修改is_recommend状态为1；
        $hot_search = D("Novel")->field('id,name,img,description')->order("click_count desc")->where("is_shelf = 1")->limit("0,5")->select();
        $hot_searchbooks = [];
        foreach($hot_search as $v){
            $hot_searchbooks[] = $v['id'];
        }
        if (isset($shelfbooksId)&&in_array($novel_id,$hot_searchbooks)) {
            $updateData['is_recommend'] = 1;
            $id = M("bookshelf")->where("id = '$shelfbooksId' and open_id = '$open_id'")->save($updateData);
        }else{
            $id = D('bookshelf')->add($data);
        }

        if ($id) {
            $res['status'] = 1;
            $this->ajaxReturn($res);
        }
    }


    //最近阅读
    public function myhistory(){
        $this->is_session();
        $open_id = $_SESSION['user']['open_id'];
        //$open_id = 'o4-hqwtV62XDYP8lcmgtLrmOCZEM';
        //查看历史记录的小说详情
        $rows = D()->query("select a.*,b.* from qw_read_history as a left join qw_novel as b on a.novel_id = b.id where a.open_id = '$open_id'order by addtime desc limit 10");
        //没看到最后加点
        foreach($rows as $key=>$v){
            //$dian = $this->getdian($v['novel_id']);
            $novel_id = $v['novel_id'];
            $curl_chapter_id = $v['chapter_id'];
            $last_chapter= D('novel_chapter')->where("novel_id = $novel_id")->order('id desc')->find();
            //dump($last_chapter);die;
            $last_chapter_id = $last_chapter['id'];
            if($curl_chapter_id == $last_chapter_id){
                $rows[$key]['dian']= 0;
                $rows[$key]['tishi']='更新至 '.$last_chapter['chapter'].' '.$last_chapter['title'];
            }else{
                $rows[$key]['dian']= 1;
                $rows[$key]['tishi']='更新至 '.$last_chapter['chapter'].' '.$last_chapter['title'];
            }

        }
        $this->assign('open_id',$open_id);
        $this->assign('rows',$rows);
        $this->display();
    }

    public function Novelsign(){
        $this->is_session();
        $open_id = $_SESSION['user']['open_id'];
        //查看用户的所有标签
        $signs = M("novel_sign")->where("open_id = '$open_id'")->select();
        $signs_count = M("novel_sign")->where("open_id = '$open_id'")->count();
//		var_dump($signs);exit;
        $this->assign('signs',$signs);
        $this->assign('signs_count',$signs_count);
        $this->display();
    }

    public function Signdel(){
        $id = I('post.id');
        if (M("novel_sign")->where("sign_id = $id")->delete()) {
            $this->ajaxReturn(array(
                'status'=>1
            ));
        }else{
            $this->ajaxReturn(array(
                'status'=>1
            ));
        }
    }




}